package artid.com.arttogo.view.horizonlistview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollecterActivity;
import artid.com.arttogo.bean.ArtistFollow;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.MyBitmapImageViewTarget;
import artid.com.arttogo.view.CircleImageView;
import artid.com.arttogo.view.pulllist.PullableListView;
import artid.com.arttogo.view.pulllist.PullableScrollView;

/**
 * 项目名称：ArtworkArt
 * 类名称：NewartistArtistList_Adapter
 * 类描述：艺术家列表适配器（竖向的ListView）
 * 创建人：耿卫斌
 * 创建时间：2017-1-4 下午3:24:07
 */
public class NewartistArtistList_Adapter extends BaseAdapter implements HorizonListviewNestingListView.OnScrollListener, HorizonListviewNestingListView.OnDispatchTouchEvent {
    private Context mContext;
    private List<NewartistArtistListParser.NewartistArtistList_ArtistList> list;
    private Boolean isListView;//判断是使用的PullableScrollView还是PullableListView

    public NewartistArtistList_Adapter(Context mContext,
                                       List<NewartistArtistListParser.NewartistArtistList_ArtistList> list, Boolean isListView) {
        super();
        this.mContext = mContext;
        this.list = list;
        this.isListView = isListView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = LayoutInflater.from(mContext).inflate(R.layout.item_newartistartistlist, null);
            holder.newartistartistlist_RL = (RelativeLayout) v.findViewById(R.id.item_newartistartistlist_RL);
            holder.head_img = (CircleImageView) v.findViewById(R.id.item_newartistartistlist_head);
            holder.name = (TextView) v.findViewById(R.id.item_newartistartistlist_name);
            holder.worksNum = (TextView) v.findViewById(R.id.item_newartistartistlist_worksNum);
            holder.followNum = (TextView) v.findViewById(R.id.item_newartistartistlist_followNum);
            holder.follow = (ImageView) v.findViewById(R.id.item_newartistartistlist_follow);
            holder.mHorizontalListView = (HorizonListviewNestingListView) v.findViewById(R.id.item_newartistartistlist_HorizontalListView);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.mHorizontalListView.setOnScrollListener(this);
        holder.mHorizontalListView.setOnDispatchTouchEvent(this);
        Glide.with(mContext).load(list.get(position).getArtistimg()).asBitmap().centerCrop()
                .placeholder(R.drawable.ic_menu_rotate).error(R.drawable.ic_menu_rotate)
                .into(new MyBitmapImageViewTarget(holder.head_img));
        holder.newartistartistlist_RL.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(mContext, CollecterActivity.class);
                intent.putExtra("name", list.get(position).getRealname());
                intent.putExtra("id", list.get(position).getArtist_id());
                mContext.startActivity(intent);
            }
        });
        holder.name.setText(list.get(position).getRealname());
        holder.worksNum.setText(list.get(position).getArtNum() + "件作品");
        holder.followNum.setText(list.get(position).getFollowNum() + "人关注");
        if (list.get(position).getIs_follow() == 0) {
            //未关注
            holder.follow.setImageResource(R.drawable.ysj_btn_guanzhu);
        } else if (list.get(position).getIs_follow() == 1) {
            //已关注
            holder.follow.setImageResource(R.drawable.ysj_btn_guanzhu_sel);
        }
        holder.follow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//				ToastUtil.toast(mContext, "点击关注了!!关注状态："+list.get(position).getIs_follow());
                if (list.get(position).getIs_follow() == 0) {
                    postFollow("1", position);
                } else if (list.get(position).getIs_follow() == 1) {
                    postFollow("2", position);
                }
            }
        });
        holder.mHorizontalListView.setAdapter(new NewartistArtistHorizontalList_Adapter(mContext, list.get(position).getArtInfo()));
        holder.head_img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(mContext, CollecterActivity.class);
                intent.putExtra("id", list.get(position).getArtist_id());

                mContext.startActivity(intent);
            }
        });
        return v;
    }

    class ViewHolder {
        RelativeLayout newartistartistlist_RL;
        CircleImageView head_img;
        ImageView follow;
        TextView name, worksNum, followNum;
        HorizonListviewNestingListView mHorizontalListView;
    }

    /**
     * 关注艺术家
     *
     * @param artist_id 艺术家ID
     * @param state     1设置，2取消
     */
    private void postFollow(final String state, final int position) {
        VolleyBasic.getInstance((Activity) mContext).post(Cache.artistFollow,
                VolleyMap.getInstance().artistFollow(AppUtil.getUser().getToken(), list.get(position).getArtist_id(), state), ArtistFollow.class,
                new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        ArtistFollow itemArtistFollow = (ArtistFollow) vd.getData();
                        if ("1".equals(itemArtistFollow.getState())) {
                            if ("1".equals(state)) {
                                list.get(position).setIs_follow(1);
                            } else {
                                list.get(position).setIs_follow(0);
                            }
                            notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    private int startX = 0;//手指按下的初始位置
    private Boolean isSlide = false;//是否在滑动

    @Override
    public void onDTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
            // 记录用户手指点击的位置
            case MotionEvent.ACTION_DOWN:
                startX = (int) event.getX();
                if (isListView) {
                    PullableListView.isSlide = false;
                } else {
                    PullableScrollView.isSlide = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isListView) {
                    PullableListView.isSlide = true;
                } else {
                    PullableScrollView.isSlide = true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                //判断手指左右滑动
//			Log.d("滑动冲突监听：", (event.getX()-startX)+"==="+(startX-event.getX())+"==="+(startX-event.getX())+"==="+(event.getX()-startX));
                if ((((event.getX() - startX) > 10 && (startX - event.getX()) < 10) || ((startX - event.getX() > 10) && (event.getX() - startX) < 10)) || isSlide) {
                    if (isListView) {
                        PullableListView.isSlide = false;
                    } else {
                        PullableScrollView.isSlide = false;
                    }
                } else {
                    if (isListView) {
                        PullableListView.isSlide = true;
                    } else {
                        PullableScrollView.isSlide = true;
                    }
                }
                break;
        }
    }

    @Override
    public void onScrollStateChanged(HorizonListviewNestingListView view,
                                     int scrollState) {
        // TODO Auto-generated method stub
        Log.d("log", "onScrollStateChanged：" + scrollState);
        if (scrollState != 0) {
            isSlide = true;
        } else {
            isSlide = false;
        }
    }

    @Override
    public void onScroll(HorizonListviewNestingListView view,
                         int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
    }
}
